<template>
    <view class="my-unit flex">
       
         <image class="unit-image" :src="info.image" mode="aspectFit"></image>
   
       <view class="unit-content">
         <view class="unit-header">
           <text class="title">{{info.storeName}}</text>
         </view>
         <view class="unit-body">
           <text>页数：{{info.pageNum}}页  </text>
           <text style="margin-left: 15px;">打印次数：{{info.sales}}次</text>
         </view>
         <view class="unit-footer">
           
           <view class="quantity-control">
             <text @click="decrement" v-if="returnQuantity() > 0" class="button">-</text>
             <text v-if="returnQuantity() > 0">{{returnQuantity()}}</text>
             <text @click="increment" class="button">+</text>
           </view>
         
         </view>
       </view>
     </view>
</template>
	
<script>
	  import cookie from "@/utils/store/cookie";
	import { postCartAdd,getCartCount } from "@/api/store";
    export default{
		props: {
			// 开启动画
			info: {
				type: Object
			},

		},
        data() {
            return {
				  quantity: 0, // 添加和减少数量的状态
            }
        },
		onLoad() {
			this.quantity = this.info.quantity
			console.log(this.info.quantity)
		},
        methods:{
			returnQuantity(){
				return this.info.quantity
			},
			 doDel() {
			            this.$store.commit("switch_loading");
			        },
			        increment() {
			            // 增加数量
						this.add(1)
			        },
			        decrement() {
			           
						this.add(0)
			        },
					add(i){
						if(cookie.get("printIp")==''){
							uni.showToast({
								title: '请选择打印机',
								icon: 'error',
								duration: 2000
							});
							return;
						}
						let q = {
							productId: this.info.id,
							cartNum: i,
							pdf_path:this.info.image,
							ip: cookie.get("printIp"),
							color: "black",
							paper:"single",
							papersize:"A4"
						};
						let that = this;
						postCartAdd(q)
							.then(function(res) {
							
								if(i==1){
									uni.showToast({
										title: '添加成功',
										icon: 'success',
										duration: 2000,
										complete: () => {
											that.getCartCount(true);
										}
									});
									 that.info.quantity = that.info.quantity + 1;
								}else{
									if (that.info.quantity > 0) {
									    that.info.quantity = that.info.quantity - 1; // 减少数量
									}
								}
								console.log(that.info.quantity)
									that.$forceUpdate()
								
							})
							.catch(error => {
								
								uni.showToast({
									// title: error.response.data.msg,
									title: '添加失败',
									icon: 'none',
									duration: 2000
								});
							});
					},
					//获取购物车数量
					getCartCount: function(isAnima) {
						let that = this;
						
							getCartCount({
								numType: 0
							}).then(res => {
					
							that.$emit('transfer',res.data.count)
							});
						
						
					},
        },
		computed:{
			
		},
		created() {
			
		},
    }
</script>
<style lang='scss'>
	.unit-footer{
		display: flex;
		justify-content: flex-end;
		    margin-top: 10px;
	}
	.quantity-control{
		
	}
	.unit-header{
		display: flex;
		justify-content: start;
	}
	.unit-body{
		 margin-top: 10px;
		    font-size: 15px;
		    color: #a3a3a3;
		    display: flex;
		    justify-content: start;
		
	}
   .my-unit {
     display: flex;
	 padding:10px;
	 height: 170px;
	 background-color: #fff;
	 border-bottom: 2px solid #eee;
   }
   
   .unit-image {
     width: 100px; /* 定义图片宽度 */
     height: auto; /* 定义图片高度, 或使用具体高度值 */
   }
   
   .unit-content {
     flex: 1; /* 余下的部分用于内容显示 */
     padding: 10px;
   }
   
   .quantity-control {
     display: flex;
     align-items: center;
   }
   
   .quantity-control .button {
     padding: 5px;
     margin: 0 5px;
     border: 1px solid #ccc; /* 可以调整为你喜欢的样式 */
     cursor: pointer; /* 手型光标 */
   }
   
   .btn {
     float: right;
   }

</style>